<template>
  <div class="list-content">
    <el-row type="flex" justify="space-between">
      <el-form class="serach-form" :inline="true" :model="formInline" @submit.native.prevent >
        <el-form-item v-if="hasFather">
          <div class="iconfont icon-return return-btn" @click="returnFun" title="返回">{{ organizationName }}</div>
        </el-form-item>
        <el-form-item>
          <el-input
              v-model.trim="formInline.name" 
              placeholder="请输入路线名称"
              @keyup.enter.native="getList(1)"
              clearable>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="form-search-btn" type="primary" @click="getList(1)">查询</el-button>
        </el-form-item>
      </el-form>
      <div class="more-btns" v-if="$store.state.buttonPermission[$store.state.currentTabName].indexOf('inspectionManager') > -1">
        <el-button type="primary" @click="addPlan()">新增</el-button>
      </div>
    </el-row>
    <el-table
      v-loading="loading"
      :data="tableData"
      ref="mainTable"
      style="width: 100%">
      <el-table-column
      label="序号"
      type="index"
      width="50">
      </el-table-column>
      <el-table-column
      prop="routeName"
      label="巡检路线名称">
      </el-table-column>
      <el-table-column
        label="创建时间">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
      prop="routeTypeName"
      label="类型">
      </el-table-column>
      <el-table-column
      prop="dutyUserName"
      label="责任人">
      </el-table-column>
      <el-table-column
      prop="remark"
      label="备注">
      </el-table-column>
      <el-table-column 
      label="操作"
      width="140">
        <template slot-scope="scope">
          <el-button  @click="showDetail(scope.$index, scope.row)" type="text" size="small">详情</el-button>
          <template v-if="$store.state.buttonPermission[$store.state.currentTabName].indexOf('inspectionManager') > -1">
            <span class="dividing-line">|</span>
            <el-button @click="addPlan(scope.$index, scope.row)" type="text" size="small">修改</el-button>
            <span class="dividing-line">|</span>
            <el-button @click="delPlan(scope.$index, scope.row)" type="text" size="small">删除</el-button>
          </template>
        </template>
      </el-table-column>
      <!-- 展开详情 -->
      <el-table-column type="expand" width="1">
        <template slot-scope="scope">
          <div class="detail-content">
            <span style="display:none;">{{freashFlag}}</span> 
            <p><b>巡检路线名称：</b>{{scope.row.routeName}}</p>
            <p><b>巡检类型：</b>{{scope.row.routeTypeName}}</p>
            <p><b>责任人：</b>{{scope.row.dutyUserName}}</p>
            <p>
              <b>包含巡检点位：</b>
              <span v-if="detailObj[scope.row.id] && detailObj[scope.row.id].length>0"><span v-for="(item, index) in detailObj[scope.row.id]" :key="index">{{item||''}}&nbsp;&nbsp;&nbsp;&nbsp;</span></span>
              <span v-else>暂无数据</span>
            </p>
            <p><b>备注：</b>{{scope.row.remark}}</p>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @size-change="sizeChange"
      @current-change="pageChange"
      :current-page="formInline.currentPageIndex"
      :page-sizes="[10, 50, 100]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="formInline.currentPageTotal">
    </el-pagination>
    <el-dialog
      :close-on-click-modal="false"
      title="巡检路线维护"
      width="600px"
      top='10vh'
      v-if="dialogFalg"
      :visible.sync="dialogFalg"
      @close="closeDialog"
      v-dialogDrag>
      <edit-form :editInfo="editInfo" :closeDialog="closeDialog" :organizationId="organizationId"></edit-form>
    </el-dialog>
  </div>
</template>

<script>
import {getInspectRouteList,getInspectPointNewList,delInspectRoute } from '@/api/inspect'
import { getOrgList} from '@/api/org'
import util from '@/utils/util'
import editForm from '../Dialog/InspectAreaForm'

export default {
  name: 'InspectAreaForm',
  props: {
      organizationId: {
          required: false
      },
      organizationName: {
          default () {
            return ''
          }
      },
      hasFather: {
          type: Boolean,
          default () {
              return false
          }
      },
      returnFun: {
          type: Function,
          default () {
              return ()=>{}
          }
      }
  },
  components: { 
    editForm
  },
  data () {
    return {
      freashFlag:0,
      formInline: {
        orgId: this.organizationId || this.$store.state.userinfo.org.id,
        name:'',
        currentPageSize: 10,
        currentPageIndex: 1,
        currentPageTotal: 0
      },
      util:util,
      isMuilUnitUser:this.$store.state.userinfo.isMuilUnitUser,
      accountName:this.$store.state.userinfo.accountName,
      defaultImg: this.GLOBAL.defaultImg,
      tableData: [],//表格数据
      detailObj:{},//展开详情
      loading: false,
      //orgOptions: [{organizationId: this.$store.state.userinfo.organizationIds,shortName: "业主单位"}],
      dialogFalg: false,
      editInfo: {  }// 编辑对应的表单信息
    }
  },
  created:function() {
      this.getList(1)
      //绑定下拉框
      /*if(this.isMuilUnitUser)
        getOrgList({queryType: 0}).then((data)=>{
            this.orgOptions.push.apply(this.orgOptions,data.data);
        })   */ 
  },
  methods: {
    sizeChange(_pageSize) {//页码切换
      this.getList(1,_pageSize) 
    },
    pageChange(_pageIndex) {//翻页
      this.getList(_pageIndex) 
    },
    getList(_pageIndex, _pageSize) {//获取列表
      if(_pageIndex){
        this.formInline.currentPageIndex = _pageIndex
      }
      if(_pageSize){
          this.formInline.currentPageSize = _pageSize
      }
      this.loading =true;
      getInspectRouteList({
        queryType : 1,
        orgIds: this.formInline.orgId,
        pageIndex: this.formInline.currentPageIndex -1,
        pageSize: this.formInline.currentPageSize,
        name: this.formInline.name.trim(),
      }).then((result) => {
        this.formInline.currentPageTotal = result.total;
        this.tableData = result.data;
        this.loading =false;
      }) 
    },
    showDetail(index, row) {//展开详情
      const $table = this.$refs.mainTable
      row.isExpand = !row.isExpand;//给row一个自定义展开状态
      $table.toggleRowExpansion(row)
      if(row.isExpand){
        this.loading =true;
        let that = this
        getInspectPointNewList({
          orgIds:this.formInline.orgId,
          queryType:0,
        }).then((result) => {
          var arr = row.pointIds.split(",");
          let dataList = [];
          $.each(result.data,function(index,val){
            for(var i=0; i<arr.length; i++){
              if(arr[i] == val.id){
                dataList.push(val.name);
              }
            }
          });
          that.detailObj[row.id]= dataList
          that.freashFlag++
          that.loading =false
        })
      }
    },
    addPlan(index, row) {
      this.dialogFalg = true
      this.editInfo = row||{}
    },
    delPlan(index, row) {
      let that = this
      this.$confirm("删除该巡检路线可能会影响现有巡检流程及巡检记录，确定删除？", '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        roundButton: true
      }).then(() => {
        delInspectRoute({id: row.id}).then(res => {
           if( res.status === 200) {
            if( res.data === 200011){
              this.$message({
                message: '该路线已经被计划关联，不能删除！',
                type: 'warning'
              });
            }else{
              that.$message({
                type: 'success',
                message: '删除成功!'
              })
              that.getList(1)
            }
          }else{
            that.$message.error(res.info);
          }
        })
      }).catch(() => {

      })
    },
    closeDialog(refresh) {
      this.dialogFalg = false
      if(refresh) {
        this.getList(this.editInfo.id?'':1)
      }
    },
  },
}
</script>